package com.shenganyuan.smac.service.location;

import android.content.Intent;

import com.amap.api.location.AMapLocation;
import com.amap.api.location.AMapLocationClient;
import com.amap.api.location.AMapLocationClientOption;
import com.amap.api.location.AMapLocationListener;
import com.oklib.utils.Logger.Logger;
import com.oklib.utils.calculate.ArithUtil;
import com.shenganyuan.smac.activity.MainActivity;
import com.shenganyuan.smac.common.beans.LocationInfo;

import static com.shenganyuan.smac.common.Config.POSITION_AUTO_SERVICE_INTERVAL;

/**
 * 包名： com.amap.locationservicedemo
 * <p>
 * 创建时间：2016/10/27
 * 项目名称：LocationServiceDemo
 *
 * @author guibao.ggb
 * @email guibao.ggb@alibaba-inc.com
 * <p>
 * 类说明：后台服务定位
 * <p>
 * <p>
 * modeified by liangchao , on 2017/01/17
 * update:
 * 1. 只有在由息屏造成的网络断开造成的定位失败时才点亮屏幕
 * 2. 利用notification机制增加进程优先级
 * </p>
 */
public class LocationService extends NotiService {
    private AMapLocationClient mLocationClient;
    private AMapLocationClientOption mLocationOption;
    private int locationCount;
    /**
     * 处理息屏关掉wifi的delegate类
     */
    private IWifiAutoCloseDelegate mWifiAutoCloseDelegate = new WifiAutoCloseDelegate();
    /**
     * 记录是否需要对息屏关掉wifi的情况进行处理
     */
    private boolean mIsWifiCloseable = false;


    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        super.onStartCommand(intent, flags, startId);
        applyNotiKeepMech(); //开启利用notification提高进程优先级的机制

        if (mWifiAutoCloseDelegate.isUseful(getApplicationContext())) {
            mIsWifiCloseable = true;
            mWifiAutoCloseDelegate.initOnServiceStarted(getApplicationContext());
        }
        startLocation();
        return START_STICKY;
    }


    @Override
    public void onDestroy() {
        unApplyNotiKeepMech();
        stopLocation();
        super.onDestroy();
    }

    /**
     * 启动定位
     */
    void startLocation() {
        stopLocation();
        if (null == mLocationClient) {
            mLocationClient = new AMapLocationClient(this.getApplicationContext());
        }
        mLocationOption = new AMapLocationClientOption();
        // 使用连续
        mLocationOption.setOnceLocation(false);
        mLocationOption.setLocationCacheEnable(false);
        // 每10秒定位一次
        mLocationOption.setInterval(POSITION_AUTO_SERVICE_INTERVAL);
        // 地址信息
        mLocationOption.setNeedAddress(true);
        mLocationClient.setLocationOption(mLocationOption);
        mLocationClient.setLocationListener(new AMapLocationListener() {
            @Override
            public void onLocationChanged(AMapLocation aMapLocation) {
                //发送结果的通知
                sendLocationBroadcast(aMapLocation);
                if (!mIsWifiCloseable) {
                    return;
                }
                if (aMapLocation.getErrorCode() == AMapLocation.LOCATION_SUCCESS) {
                    mWifiAutoCloseDelegate.onLocateSuccess(getApplicationContext(), PowerManagerUtil.getInstance().isScreenOn(getApplicationContext()), NetUtil.getInstance().isMobileAva(getApplicationContext()));
                } else {
                    mWifiAutoCloseDelegate.onLocateFail(getApplicationContext(), aMapLocation.getErrorCode(), PowerManagerUtil.getInstance().isScreenOn(getApplicationContext()), NetUtil.getInstance().isWifiCon(getApplicationContext()));
                }
            }
        });
        mLocationClient.startLocation();
    }

    /**
     * 停止定位
     */
    private void stopLocation() {
        if (null != mLocationClient) {
            mLocationClient.stopLocation();
        }
    }

    private void sendLocationBroadcast(AMapLocation aMapLocation) {
        //记录信息并发送广播
        locationCount++;
        long callBackTime = System.currentTimeMillis();
        StringBuffer sb = new StringBuffer();
        sb.append("定位完成 第" + locationCount + "次\n");
        sb.append("回调时间: " + Utils.formatUTC(callBackTime, null) + "\n");
        Intent mIntent = new Intent(MainActivity.RECEIVER_ACTION);
        if (null == aMapLocation) {
            sb.append("定位失败：location is null!!!!!!!");
        } else {
            sb.append(Utils.getLocationStr(aMapLocation));
            double longitude = aMapLocation.getLongitude();
            double latitude = aMapLocation.getLatitude();
            longitude = ArithUtil.round(longitude, 6);
            latitude = ArithUtil.round(latitude, 6);
            Logger.e("-----" + longitude + "====" + latitude);
            if (Double.doubleToLongBits(longitude) != Double.doubleToLongBits(0.0) && Double.doubleToLongBits(latitude) != Double.doubleToLongBits(0.0)) {
                LocationInfo locationInfo = new LocationInfo();
                locationInfo.longitude = longitude + "";
                locationInfo.latitude = latitude + "";
                locationInfo.locationTime = aMapLocation.getTime();
                mIntent.putExtra("locationInfo", locationInfo);
            }
        }
        mIntent.putExtra("result", sb.toString());
        //发送广播
        sendBroadcast(mIntent);
    }

}
